package com.djx.service.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @ClassName DirectMqConfiguration
 * @Date 2021/6/8 0008 上午 9:15
 * @Author Administrator
 * @Description TODO
 * Version 1.0
 */
@Configuration
public class TTLMqConfiguration {

    /**
     * 交换机名字
     */
    String exchangeName = "TTL_direct_exchange";
    /**
     * 队列名
     */
    String smsDirectQueueName = "TTL_sms_direct_queue";
    String emailDirectQueueName = "TTL_email_direct_queue";
    String weChatDirectQueueName = "TTL_weChat_direct_queue";

    /**
     * 1.创建交换机
     *
     * @return
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(exchangeName, true, false);
    }


    /**
     * 创建队列
     *
     * @return
     */
    @Bean
    public Queue smsDirectQueue() {
        // 设置队列过期时间
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-message-ttl",10000); //五秒  int 类型

        return new Queue(smsDirectQueueName, true,false,false,args);
    }

    @Bean
    public Queue emailDirectQueue() {
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-message-ttl",10000); //五秒  int 类型

        return new Queue(emailDirectQueueName, true,false,false,args);
    }

    @Bean
    public Queue weChatDirectQueue() {
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-message-ttl",10000); //五秒  int 类型


        return new Queue(weChatDirectQueueName, true,false,false,args);
    }

    @Bean Queue tTLpuTongQueue(){

        return new Queue("TTL_puTong_queue", true,false,false);

    }

    /**
     * 交换机绑定队列设置路由key
     * @return
     */
    @Bean
    public Binding puTongDirectBinding() {
        return BindingBuilder.bind(tTLpuTongQueue()).to(directExchange()).with("pt");
    }

    @Bean
    public Binding smsDirectBinding() {
        return BindingBuilder.bind(smsDirectQueue()).to(directExchange()).with("sms");
    }

    @Bean
    public Binding emailDirectBinding() {
        return BindingBuilder.bind(emailDirectQueue()).to(directExchange()).with("email");
    }

    @Bean
    public Binding weChatDirectBinding() {
        return BindingBuilder.bind(weChatDirectQueue()).to(directExchange()).with("weChat");
    }

}
